<template>
  <div>
    <!-- 位置栏 -->
    <div class="location-bar">
      <div>位置 :&ensp;</div>
      <div>一级页面</div>
      <div style="color: #000">/二级页面</div>
    </div>
    <!-- 查询框 -->
    <div class="search-bar">
      <a-form-model layout="inline" :model="searchForm" @submit="searchSubmit" @submit.native.prevent>
        <a-form-model-item label="姓名:">
          <a-input v-model="searchForm.name" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="创建日期:">
          <a-range-picker valueFormat="yyyy-MM-DD HH:mm:ss" v-model="searchForm.dateRange" />
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" html-type="submit">查询</a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
    <!-- 内容框 -->
    <div class="content-box">
      <!-- 新增 -->
      <a-button type="primary" @click="add_modal"> 新增 </a-button>
      <!-- 表格 -->
      <a-table :columns="columns" :data-source="data" @change="handleTableChange" :pagination="pagination" :row-key="(record) => record.id">
        <span slot="via" slot-scope="text,record"><img :src="cdn + text" alt="" srcset="" style="width: 50px" @click="imgshow(record)" /></span>
        <span slot="action" slot-scope="text, record">
          <a @click="modify_modal(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="del(record)"> 删除 </a>
        </span>
      </a-table>
      <!-- 新增弹窗 -->
      <a-modal :visible="add_visible" title="新增专家智库" okText="确认" cancel-text="取消" @ok="add_ok" @cancel="add_cancel">
        <a-form-model :model="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }" :rules="rules" ref="addForm">
          <a-form-model-item label="图片" prop="via">
            <upload-widget v-model="form.via"></upload-widget>
          </a-form-model-item>
          <a-form-model-item label="姓名" prop="name">
            <a-input v-model="form.name" />
          </a-form-model-item>
          <a-form-model-item label="专家职位" prop="position">
            <a-input v-model="form.position" />
          </a-form-model-item>
          <a-form-model-item label="排序值">
            <a-input v-model="form.orderby" />
          </a-form-model-item>
          <a-form-model-item label="简介" prop="introduction">
            <a-input v-model="form.introduction" />
          </a-form-model-item>
        </a-form-model>
      </a-modal>
      <!-- 编辑弹窗 -->
      <a-modal :visible="edit_visible" title="新增专家智库" okText="确认" cancel-text="取消" @ok="edit_ok" @cancel="edit_cancel">
        <a-form-model :model="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 16 }" :rules="rules" ref="editForm">
          <a-form-model-item label="图片" prop="via">
            <upload-widget v-model="form.via"></upload-widget>
          </a-form-model-item>
          <a-form-model-item label="姓名" prop="name">
            <a-input v-model="form.name" />
          </a-form-model-item>
          <a-form-model-item label="专家职位" prop="position">
            <a-input v-model="form.position" />
          </a-form-model-item>
          <a-form-model-item label="排序值">
            <a-input v-model="form.orderby" />
          </a-form-model-item>
          <a-form-model-item label="简介" prop="introduction">
            <a-input v-model="form.introduction" />
          </a-form-model-item>
        </a-form-model>
      </a-modal>
      <!-- 查看大图 -->
      <a-modal :visible="image" :footer="null" :closable="false" :maskClosable="true" @cancel="image_cancel">
        <div style="text-align: center"><img :src="cdn + imgs" alt="" srcset="" style="width: 400px" /></div>
      </a-modal>
    </div>
  </div>
</template>

<script>
const columns = [
  {
    title: '序号',
    dataIndex: 'id',
    key: 'id',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '头像',
    dataIndex: 'via',
    key: 'via',
    scopedSlots: { customRender: 'via' },
  },
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    scopedSlots: { customRender: 'name' },
  },
  {
    title: '专家职位',
    dataIndex: 'position',
    key: 'position',
    scopedSlots: { customRender: 'position' },
  },
  {
    title: '简介',
    key: 'introduction',
    dataIndex: 'introduction',
    scopedSlots: { customRender: 'introduction' },
  },
  {
    title: '排序',
    key: 'orderby',
    dataIndex: 'orderby',
    scopedSlots: { customRender: 'orderby' },
  },
  {
    title: '创建日期',
    key: 'created',
    dataIndex: 'created',
    scopedSlots: { customRender: 'created' },
  },
  {
    title: '操作',
    key: 'action',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
  },
];
export default {
  data() {
    return {
      columns,
      data: [],
      searchForm: { dateRange: ['', ''], name: '' }, //查询参数
      pagination: { pageSize: 10, total: 100, pageNum: 1, showSizeChanger: true }, //分页参数
      form: { via: '', name: '', position: '', orderby: '', introduction: '', id: '' },
      add_visible: false,
      edit_visible: false,
      del_id: '',
      image: false,
      imgs: '', //图片地址
      rules: {
        via: [{ required: true, message: '请上传图片!', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名!', trigger: 'blur' }],
        position: [{ required: true, message: '请输入职位!', trigger: 'blur' }],
        introduction: [{ required: true, message: '请输入内容!', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.loadArticleList();
  },
  methods: {
    //读取文章列表
    async loadArticleList() {
      let reqData = { searchText: this.searchForm.name, startTime: this.searchForm.dateRange[0], endTime: this.searchForm.dateRange[1] };
      let reqParam = { pageNumber: this.pagination.pageNum, pageSize: this.pagination.pageSize };
      let searchUrl = `/experts/search/${reqParam.pageNumber}/${reqParam.pageSize}`;
      let result = await this.axios.post(searchUrl, reqData);
      // console.log(result);
      this.data = result.data.data;
      this.pagination.total = result.data.total; //总数
      console.log(this.data);
      this.search_empty();
    },
    //新增
    async newlyAdded() {
      let reqData = { name: this.form.name, orderby: this.form.orderby, position: this.form.position, introduction: this.form.introduction, via: this.form.via };
      let saveUrl = `/experts/save`;
      let result = await this.axios.post(saveUrl, reqData);
      console.log(result);
      this.loadArticleList();
      this.form_empty();
    },
    //编辑
    async modifyList() {
      let reqData = { name: this.form.name, orderby: this.form.orderby, id: this.form.id, position: this.form.position, introduction: this.form.introduction, via: this.form.via };
      let updateUrl = `/experts/update`;
      let result = await this.axios.post(updateUrl, reqData);
      console.log(result);
      this.loadArticleList();
      this.form_empty();
    },
    //删除
    async deleteList() {
      let reqData = {};
      let reqParam = { id: this.del_id };
      let deleteUrl = `/experts/delete/${reqParam.id}`;
      let result = await this.axios.delete(deleteUrl, reqData);
      console.log(result);
      this.loadArticleList();
    },
    //搜索
    searchSubmit() {
      this.loadArticleList();
    },
    //新增
    add_modal() {
      this.add_visible = true;
    },
    //新增取消
    add_cancel() {
      this.add_visible = false;
      this.form_empty();
    },
    //新增确定
    add_ok() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          console.log(this.form);
          this.add_visible = false;
          this.newlyAdded();
        }
      });
    },
    //编辑
    modify_modal(record) {
      this.edit_visible = true;
      this.form.id = record.id;
      this.form.via = record.via;
      this.form.name = record.name;
      this.form.position = record.position;
      this.form.orderby = record.orderby;
      this.form.introduction = record.introduction;
    },
    edit_cancel() {
      this.edit_visible = false;
      this.form_empty();
    },
    edit_ok() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          console.log(this.form);
          this.edit_visible = false;
          this.modifyList();
        }
      });
    },
    //删除
    del(record) {
      this.del_id = record.id;
      this.deleteList();
    },
    // 分页
    handleTableChange(pagination) {
      this.pagination.pageNum = pagination.current;
      this.loadArticleList();
    },
    //清空搜索
    search_empty() {
      this.searchForm.dateRange = ['', ''];
      this.searchForm.name = '';
    },
    //清空form
    form_empty() {
      this.form.id = '';
      this.form.via = '';
      this.form.name = '';
      this.form.position = '';
      this.form.orderby = '';
      this.form.introduction = '';
    },
    //点击图片
    imgshow(record) {
      this.image = true;
      this.imgs = record.via;
      // console.log(this.imgs );
    },
    image_cancel() {
      this.image = false;
    },
  },
};
</script>

<style>
.location-bar {
  display: flex;
  font-size: 14px;
  color: #88898b;
}
.search-bar {
  width: 100%;
  height: 80px;
  background-color: #ffffff;
  margin-top: 17px;
  box-sizing: border-box;
  padding: 20px 30px;
}
.content-box {
  width: 100%;
  background-color: #ffffff;
  margin-top: 20px;
  box-sizing: border-box;
  padding: 20px 30px;
}
</style>